<?php

namespace Cms\Article;

class Tag extends \Model {

    static public $table = 'cms_article_tag';
    protected $_fillable = array(
        'article_id' => 'int',
        'tag_id' => 'int',
    );
    
    public function getListByArticle($article_id) {
        $where = [
            'article_id' => $article_id,
        ];
        return Db()->getAll(static::$table)->where($where)->result();
    }
    
    public function existByArticleTag($article_id, $tag_id) {
        $where = [
            'article_id' => $article_id,
            'tag_id' => $tag_id,
        ];
        return Db()->getOne(static::$table)->where($where)->result();
    }

    public function removeByArticleTag($article_id, $tag_id) {
        $where = [
            'article_id' => $article_id,
            'tag_id' => $tag_id,
        ];
        return Db()->delete(static::$table, $where);
    }

    public function countArticleIdsByTag($tag_id) {
        $where = array(
            'tag_id' => $tag_id,
        );
        return Db()->count(static::$table)->where($where)->result();
    }

    public function getArticleIdsByTag($tag_id, $offset = null, $length = null) {
        $where = array(
            'tag_id' => $tag_id,
        );
        return Db()->getCol(static::$table)->fields('article_id')->where($where)->limit($offset, $length)->result();
    }

    public function clearByArticle($article_id) {
        $where = array(
            'article_id' => $article_id,
        );
        return Db()->delete(static::$table, $where);
    }

    public function clearByTag($tag_id) {
        $where = array(
            'tag_id' => $tag_id,
        );
        return Db()->delete(static::$table, $where);
    }

}
